/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tictactoe;

/**
 *
 * @author houfukude
 */
public class minMaxSearch {
    private static int Draw = 0;
    private static int CompWin = 1;
    private static int CompLoss = -1;
    static int mv[] = new int[]{-2,-2};
    //public static int BestMove;
    public static void minMaxSearch(int depth,MyJButton button[],int count,int mv[]){
        //mv[0] mv[1]---bestmove ,value
        //dc[0] dc[1]---dc ,res
        int dc[] = new int [2];
        
        if(depth>0){
        //if(mv[1]==1){
            for(int i=0;i<9;i++){
                if(button[i].value==0&&count<9){
                    MyTicTacToe.Move(i);
                    System.out.println("count in :"+count);
                    
                        minMaxSearch(depth-1,button,count,dc);
                    
                    MyTicTacToe.reMove(i);
                    if(count%2==0){

                        if(dc[1]<mv[1]){

                            mv[1] = dc[1];
                                mv[0] =i;
                            
                        }
                    }else{
                        if(dc[1]>mv[1]){

                            mv[1] = dc[1];
                                mv[0]=i;
                            
                        }
                    }
                }

           }
        }else{
            MyTicTacToe.Move(mv[0]);
        }

    }

    public static int[] FindCompMove(MyJButton button[]){
        //int mv[] = new int[2];
        int dc[] = new int[2];
        if(Fill(button)==9){
            mv[1] = Draw;
        }else if(ImmediateCompWin(button,mv[0])){
            mv[1] = CompWin;
        }else{
            mv[1] = CompLoss;
            for(int i=0;i<9;i++){
                if(button[i].value==0){
                    MyTicTacToe.Move(i);
                    dc = FindHumanMove(button);
                    System.out.println("AI dc:"+dc[0]+" value:"+dc[1]);
                    MyTicTacToe.reMove(i);
                    if(dc[1]>mv[1]){
                        mv[1] = dc[1];
                        mv[0] = i;
                        
                    }
                }
            }
        }
        return mv;
    }

    public static int[] FindHumanMove(MyJButton button[]){
        
        int dc[] = new int[2];
        if(Fill(button)==9){
            mv[1] = Draw;
        }else if(ImmediateHumanWin(button,mv[0])){
            mv[1] = CompLoss;
        }else{
            mv[1] = CompWin;
            for(int i=0;i<9;i++){
                if(button[i].value==0){
                    MyTicTacToe.Move(i);
                    dc = FindCompMove(button);
                    System.out.println("AI dc:"+dc[0]+" value:"+dc[1]);
                    MyTicTacToe.reMove(i);
                    if(dc[1]<mv[1]){
                        mv[1] = dc[1];
                        mv[0] = i;
                        //System.out.println("HUM bestmove:"+mv[0]+" value:"+mv[1]);
                    }
                }
            }
        }
        return mv;
    }
    private static int Fill(MyJButton button[]){
        int a = 0;
        for(int i=0;i<9;i++){
            if(button[i].value != 0){
                a++;
            }
        }
        return a;
    }
    private static boolean ImmediateCompWin(MyJButton button[],int bestMove){
        if(bestMove == -2){
            return false;
        }else{
            button[bestMove].value=1;
            int answer = MyTicTacToe.check();
            button[bestMove].value = 0;
            if(answer == 3){
                return true;
            }else{
                return false;
            }
        }
        
    }
    private static boolean ImmediateHumanWin(MyJButton button[],int bestMove){
        if(bestMove == -2){
            return false;
        }else{
            button[bestMove].value=-1;
            int answer = MyTicTacToe.check();
            button[bestMove].value = 0;
            if(answer == -3){
                return true;
            }else{
                return false;
            }
        }

    }
    public  static int Evaluate(){
        int bestA = 0,bestB = 0;
        for(int i =0;i<8;i++){
            if(MyTicTacToe.resoult(i)==1){
                bestA++;
            }
            if(MyTicTacToe.resoult(i)==-1){
                bestB++;
            }
        }
        if(MyTicTacToe.count%2==0)
        {
            System.out.println("A-B:"+(bestA-bestB));
            return bestA-bestB;
        }
        else
        {
            System.out.println("B-A:"+(bestB-bestA));
            return bestB-bestA;
        }

    }


}
